package com.zh.index.btree.model;

import java.util.Iterator;

/**
 * Created by Administrator on 2021/7/28.
 * branchNode 的内部节点
 */
public class BranchInnerNode<T extends Comparable> {

    BranchNode<T> branchNode;

    T t;

    // 左儿子
    Node<T> leftChildrenNode;

    // 右儿子 只有当前节点再父容器的最右边时才会有值
    Node<T> rightChildrenNode;

    // 下一个内部节点
    BranchInnerNode<T> nextInnerNode;

    // 上一个内部节点
    BranchInnerNode<T> preInnerNode;

    protected BranchInnerNode(Node left, Node<T> right) {
        this.t = right.t;
        this.leftChildrenNode = left;
        this.rightChildrenNode = right;
        left.belongInnerNode = this;
        right.belongInnerNode = this;
    }

    /**
     * 在当前 innerNode 右边增加 innerNode
     * 只有当前节点为最右边节点时才能调用
     * @param node
     */
    protected void addRightInnerNode(BranchInnerNode<T> node) {
        this.rightChildrenNode = null;
        node.branchNode = this.branchNode;
        node.preInnerNode = this.preInnerNode;
        node.addLeftInnerNode(this);
    }

    /**
     * 再当前 innerNode 左边增加 innerNode
     * @param node
     */
    protected void addLeftInnerNode(BranchInnerNode<T> node) {
        if (null != node.rightChildrenNode) {
            this.leftChildrenNode = node.rightChildrenNode;
            this.leftChildrenNode.belongInnerNode = this;
            node.rightChildrenNode = null;
        }
        node.branchNode = this.branchNode;

        BranchInnerNode temp = this.preInnerNode;
        this.preInnerNode = node;
        node.preInnerNode = temp;
        if (null != temp)
            temp.nextInnerNode = node;
        node.nextInnerNode = this;
        if (this.branchNode.headInnerNode == this)
            this.branchNode.headInnerNode = node;
        this.branchNode.increase();
    }

    protected Node<T> getRightChildrenNode() {
        return rightChildrenNode;
    }

    @Override
    public String toString() {
        return t.toString();
    }

    protected void deleteNode(Node<T> node) {
        if (null != this.preInnerNode)
            this.preInnerNode = this.nextInnerNode;
        if (null != this.nextInnerNode)
            this.nextInnerNode = this.preInnerNode;



        this.nextInnerNode = null;
        this.preInnerNode = null;

        branchNode.decrement();
    }
}
